/*
 *  FileMgr.mm
 *
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#import "FileMgr.h"

#define FILE_WRITE_TRUNC 3

@implementation FileMgr

+(NSString *)GetFilePath:(NSString*)fileName
{
	if ( fileName == nil )
	{
//		NSLog(@"GetFilePath : Error Param nil!");
		return nil;
	}
	
	NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, 
															  NSUserDomainMask, 
															  YES );
	NSString *docDir = [ arrayPaths objectAtIndex:0 ];
	return [docDir stringByAppendingPathComponent:fileName ];
}

+(bool)CreateFile:(NSString*)fileName
{
	NSString *filePath = [ self GetFilePath:fileName ];
	if ( filePath == nil )
	{
		//		NSLog(@"CreateFile : Error filePath nil!");
		return NO;
	}
	
	//	NSLog(@"CreateFile : success ");
	return [[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
}


+(BOOL)CreateDirectory:(NSString*)fileName
{
	//	NSLog(@"CreateFile : success ");
	return [[NSFileManager defaultManager] createDirectoryAtPath:fileName withIntermediateDirectories:YES attributes:nil error:nil];
}


+(bool)DeleteFile:(NSString*)fileName
{
	NSString *filePath = [ self GetFilePath:fileName ];
	if ( filePath == nil )
	{
//		NSLog(@"DeleteFile : Error filePath nil!");
		return NO;
	}
	
//	NSLog(@"DeleteFile : success ");
	return [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil ];
}

+(NSFileHandle*)OpenFile:(NSString*)fileName
{
	NSString *filePath = [ self GetFilePath:fileName ];
	if ( filePath == nil )
	{
//		NSLog(@"OpenFile : Error filePath nil!");
		return nil;
	}
	
//	NSLog(@"OpenFile : success ");
	return [ NSFileHandle fileHandleForUpdatingAtPath:filePath ];		// Read/Wrtie Mode
}

+(void)CloseFile:(NSFileHandle*)hFile
{
	if ( hFile == nil )
	{
//		NSLog(@"CloseFile : Error hFile nil!");
		return;
	}
	
//	NSLog(@"CloseFile : success ");
	[hFile closeFile];
}

+(int)WriteFile:(NSFileHandle*)hFile:(void*)data:(int)offset:(int)len
{
	if ( hFile == nil || data == nil || offset < 0 || len < 0 )
	{
//		NSLog(@"WriteFile : Error Param invalide!");
		return 0;
	}
	
	NSData *pData = [ NSData dataWithBytes:(const void*)data length:len ];
	[ hFile seekToFileOffset:offset ];
	[ hFile writeData:pData ];
	
//	NSLog(@"WriteFile : success ");
	return len;
}

+(int)WriteFileFullPath :(NSString*)fileName :(int)mode :(void*)data:(int)offset:(int)len
{
	NSFileHandle *hFile = nil;
	
	if (mode == FILE_WRITE_TRUNC)
	{
		if ( [[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil] == NO )
		{
			//			NSLog(@"WriteFileEx : Create File Error!");
			return 0;
		}
	}
	else
	{
		if ( ![[ NSFileManager defaultManager ] fileExistsAtPath:fileName ] )
		{
			if ( [[NSFileManager defaultManager] createFileAtPath:fileName contents:nil attributes:nil] == NO )
			{
				//			NSLog(@"WriteFileEx : Create File Error!");
				return 0;
			}
		}
	}
	
	hFile = [ NSFileHandle fileHandleForUpdatingAtPath:fileName ];
	if ( hFile == nil )
	{
		//			NSLog(@"WriteFileEx : file open Error!");
		return 0;
	}
	
	int moveOffset;
	if ( offset < 0 )
	{
		NSDictionary *attribute = [[ NSFileManager defaultManager ] attributesOfItemAtPath:fileName error:nil];
		moveOffset = [[ attribute objectForKey:NSFileSize] intValue ] - 1;		// -1¿Ã ∏¬≥™.
		if( moveOffset < 0 )
			moveOffset = 0;
	}
	else
	{
		moveOffset = offset;
	}
	
	int nWriteSize = [ self WriteFile : hFile : data : moveOffset : len ];
	[self CloseFile:hFile ];
	
	//	NSLog(@"WriteFileEx : success ");
	return nWriteSize;
}

+(int)WriteFileEx:(NSString*)fileName:(void*)data:(int)offset:(int)len
{
	NSFileHandle *hFile = nil;
	
	if ( [ self IsFileExist:fileName] )
	{
		hFile = [ self OpenFile:fileName ];
		if ( hFile == nil )
		{
//			NSLog(@"WriteFileEx : file open Error!");
			return 0;
		}
	}
	else
	{
		if ( [ self CreateFile:fileName ] == NO )
		{
//			NSLog(@"WriteFileEx : Create File Error!");
			return 0;
		}
		
		hFile = [ self OpenFile:fileName ];
		if ( hFile == nil )
		{
//			NSLog(@"WriteFileEx : file open Error!");
			return 0;
		}
	}
	
	int moveOffset;
	if ( offset < 0 )
	{
		moveOffset = [ self GetFileSize:fileName ] - 1;		// -1¿Ã ∏¬≥™.
	}
	else
	{
		moveOffset = offset;
	}
		
	int nWriteSize = [ self WriteFile:hFile:data:moveOffset:len ];
	[self CloseFile:hFile ];
	
//	NSLog(@"WriteFileEx : success ");
	return nWriteSize;
}

+(int)ReadFile:(NSFileHandle*)hFile:(void*)data:(int)offset:(int)len
{
	if ( hFile == nil || data == nil || offset < 0 || len == 0)
	{
//		NSLog(@"ReadFile : Error Param invalide!");
		return 0;
	}
	
	int nLen = 0;
	
	// len¿Ã ¿€¿∏∏È ∆ƒ¿œ ≥°±Ó¡ˆ ¿–¥¬¥Ÿ.
	if (len <  0)
	{
		NSData	 *pData = [hFile readDataToEndOfFile];
		
		// data πˆ∆€¿« ≈©±‚∞° length∫∏¥Ÿ ƒøæﬂ «—¥Ÿ.
		memset(data, 0x00, [pData length]);
		[pData getBytes:data];
		
		nLen = [pData length];
	}
	else
	{	
		memset( data, 0x00, len );	// clear 
	
		[ hFile seekToFileOffset:offset ];
		NSData *pData = [ hFile readDataOfLength:len ];
		[ pData getBytes:data length:len ];
	
		//	NSLog(@"ReadFile : success ");
		nLen = [pData length];
	}
	
	return nLen;
}


+(int)ReadFileEx:(NSString*)fileName : (void*)data : (int)offset : (int)len
{
	NSFileHandle *hFile = nil;
	
	if ( [ self IsFileExist:fileName] )
	{
		hFile = [ self OpenFile:fileName ];
		if ( hFile == nil )
		{
//			NSLog(@"ReadFileEx : file open Error!");
			return 0;
		}
	}
	else
	{	
//		NSLog(@"ReadFileEx : file not Exist!");
		return 0;
	}

	int nReadSize = [ self ReadFile : hFile : data : offset : len ];
	[self CloseFile:hFile ];
	
//	NSLog(@"ReadFileEx : success ");
	return nReadSize;
}


+(bool)IsFileExist:(NSString*)fileName
{
	NSString *filePath = [ self GetFilePath:fileName ];
	if ( filePath == nil )
	{
//		NSLog(@"IsFileExist : Error filePath nil!");
		return NO;
	}
		
    NSLog(@"%@", filePath);
	return [[ NSFileManager defaultManager ] fileExistsAtPath:filePath ];
}


+ (NSString*) GetAbsBundlePath:(NSString*) filePath
{
	NSString *bundlePath = [[NSBundle mainBundle]bundlePath];
	return [bundlePath stringByAppendingPathComponent:filePath];
}


+(int)GetFileSize:(NSString*)fileName
{
	NSString *filePath = [ self GetFilePath:fileName ];
	if ( filePath == nil )
	{
//		NSLog(@"GetFileSize : Error filePath nil!");
		return 0;
	}
	
	NSDictionary *attribute = [[ NSFileManager defaultManager ] attributesOfItemAtPath:filePath error:nil];
	
	return [[ attribute objectForKey:NSFileSize] intValue ];
}

+ (BOOL)CopySourceFile:(NSString *)sourceFile DestFile:(NSString *)destFile {
	NSString *filePath = [ self GetFilePath:destFile ];
    return [[ NSFileManager defaultManager ] copyItemAtPath:sourceFile toPath:filePath error:nil];
}

@end
 
